package com.sc.nft.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.RoughStoneOrderRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.vo.RoughStoneOrderVO;
import com.sc.nft.entity.vo.UserCollectionExcelVO;
import com.sc.nft.service.RoughStoneOrderService;
import com.sc.nft.sup.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/roughstoneorder")
@Api(tags = "MT原石订单")
public class RoughStoneOrderController {

    private final RoughStoneOrderService roughStoneOrderService;

    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "原石订单列表", notes = "原石订单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<RoughStoneOrderVO> page(@RequestBody RoughStoneOrderRequest request) {
        Page<RoughStoneOrderVO> page = roughStoneOrderService.orderPageFromAdmin(request.getOrderNo(), request.getStatus(), request.getTypeClassify(), request.getStartOrderTime(), request.getEndOrderTime(), request.getStartPayTime(), request.getEndPayTime(),request.getUserTel(), request.getPageNo(), request.getPageSize());
        return PageResult.success(page);
    }

    @SneakyThrows
    @PostMapping(value = "/roughStoneOrderExcel", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取所有订单数据", notes = "获取所有订单数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public void roughStoneOrderExcel(@RequestBody RoughStoneOrderRequest request, HttpServletResponse response) {
        List<RoughStoneOrderVO> data = roughStoneOrderService.roughStoneOrderExcel(request.getOrderNo(), request.getStatus(), request.getTypeClassify(), request.getStartOrderTime(), request.getEndOrderTime(), request.getStartPayTime(), request.getEndPayTime(),request.getUserTel());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("原石订单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserCollectionExcelVO.class).sheet("原石订单").doWrite(data);
    }
}
